package main

import (
	"encoding/json"
	"fmt"
	"github.com/garyburd/redigo/redis"
)

type User struct {
	Name string
	Age  int
}

func main() {
	RedisInit()
	defer RedisClose()
	var err error

	var user = make([]User, 0)
	user = []User{
		{Name: "叶叶子", Age: 18},
		{Name: "abc", Age: 1},
	}

	data, err := json.Marshal(user)
	_, err = Rds.Do("set", "user", data, "EX", 5) //redis set命令
	if err != nil {
		fmt.Println(err)
		return
	}

	var res = make([]User, 0)
	r, err := redis.Bytes(Rds.Do("get", "user"))
	if len(r) > 0 {
		err = json.Unmarshal(r, &res)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("使用redis", res)
	}
}
